/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package xq.gwt.ui.widgets.property;

import com.smartgwt.client.widgets.form.fields.FormItem;

import xq.gwt.mvc.controller.AbstractPropertyController;
import xq.gwt.mvc.model.PropertyModel;
import xq.gwt.mvc.view.ObservableViewImp;
import xq.gwt.mvc.view.PropertyView;


/**
 *
 * @author admin
 */
public abstract class FormItemPropertyPresenter extends ObservableViewImp implements PropertyView{
    protected FormItem formItem;
    protected AbstractPropertyController controller;
    private String errorMessage;
          
    public void setError(String errorMessage) {    	
    	if(errorMessage == null){
    		clearError();
    		return;
    	}
    	
    	if(errorMessage.equals(this.errorMessage))
    		return;    	
    	
    	if(formItem.getForm()!= null){    		
    		this.errorMessage = errorMessage;
    		formItem.getForm().setFieldErrors(formItem.getName(),this.errorMessage, true);    		
    	}
    }

    public void clearError() {
    	if(formItem.getForm()!= null){    		
    		if(errorMessage != null)
    			formItem.getForm().clearFieldErrors(formItem.getName(), true);
    	}
    	errorMessage = null;
    }

    public void setEnabled(boolean enabled) {    	
    	formItem.setDisabled(!enabled);
    }


    public void setRequired(boolean required) {
        formItem.setRequired(required);
    }
    	
	public void setName(String name) {
		if(name == null)
			return;
		if(name.contains(" ")){
			String modifiedName = name.replaceAll("", "");
			formItem.setName(modifiedName);
			return;
		}
			
		formItem.setName(name);
		
	}
	
	public void setTitle(String title) {
		formItem.setTitle(title);
		
	}
    
	public FormItem getFormItem() {
		return formItem;
	}
    
	public void validate(){
		if(controller != null) 
			controller.validate();
	}
		
	public PropertyModel getModel(){
		if (controller == null){
			return null;
		}
		return controller.getModel();
	}

}
